package com.etrans.web.gps.business.enterpriseTask.timer;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.etrans.web.gps.business.enterpriseTask.domian.EnterpriseSwap;
import com.etrans.web.gps.business.enterpriseTask.domian.EnterpriseVo;
import com.etrans.web.gps.business.enterpriseTask.service.SendEntMessageService;
import com.etrans.web.gps.util.DateUtil;
public class SendEntMessageTask {

	private static final Logger logger = LoggerFactory.getLogger(SendEntMessageTask.class);

	@Autowired
	private SendEntMessageService sendMessageService;
	
	

	/**
	 * 每天定时短信发送
	 */
	public void sendMessageDay() {

		System.out.println("*******************短信发送********************");
	}

	/**
	 * 每周定时发送信息
	 */
	public void sendMessageWeek() {
		
		try {
			// 获取当前日期的前一周
			String startStr = DateUtil.getDateTimeString(DateUtil.getPreviousWeekDate(),"yyyy-MM-dd");
			String endStr = DateUtil.getDateToString(DateUtil.getPreviousWeekSunDateDate(),"yyyy-MM-dd");
			String msgTime = "(" + startStr + "至" + endStr + ")业户周考核统计";
			long curr = System.currentTimeMillis();
			logger.info("业户周考核定时通知启动"+msgTime);
			// 查询所有的业户
			Map<String,Object> param = new HashMap<>();
			
			List<EnterpriseVo> entList = sendMessageService.queryAllEnterprise(param);
			List<EnterpriseSwap> entSwapList = sendMessageService.queryEnterpriseSwapByWeek(startStr);
			String Time = "week";
			sendMessageService.sendSMS(entList,entSwapList,msgTime);
			boolean flag=true;
			sendMessageService.sendMail(entList,entSwapList,msgTime,Time,flag);
			logger.info("业户周考核定时通知完成:历时"+ String.valueOf(System.currentTimeMillis()-curr)+"秒");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	/**
	 * 每月定时发送信息
	 */
	public void sendMessageMonth() {
		try {
			String previousMonth =DateUtil.getDateToString(DateUtil.getPreviousMonthDate(1),"yy-MM");
			String msgTime = "("+previousMonth+"月)业户月考核统计";
			long curr = System.currentTimeMillis();
			logger.info("业户月考核定时通知启动"+msgTime);
			// 查询所有的业户
			Map<String,Object> param = new HashMap<>();
			List<EnterpriseVo> entList = sendMessageService.queryAllEnterprise(param);
			List<EnterpriseSwap> entSwapList = sendMessageService.queryEnterpriseSwapByMonth(previousMonth);
			String Time = "month";
			boolean flag = true;
			sendMessageService.sendSMS(entList,entSwapList,msgTime);
			sendMessageService.sendMail(entList,entSwapList,msgTime,Time,flag);
			logger.info("业户月考核定时通知完成:历时"+ String.valueOf(System.currentTimeMillis()-curr)+"秒");
		} catch (Exception e) {
			e.printStackTrace();
			logger.info("业户月考核发送短信失败:"+e.getMessage());
		}
	}


}
